﻿using OrderSystem;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    public class Service1 : IService1 // 实现服务接口
    {
        private IService1Callback callback; // 指向客户端的回调通道，服务使用该回调通道通过回调接口将消息发送回客户端
        public Service1()
        {
            //
            OperationContext context = OperationContext.Current; // 获取当前OperationContext
            callback = context.GetCallbackChannel<IService1Callback>(); // 通过OperationContext获得客户端回调通道
            //
        }

        public void EventDelete(string selectedItem)
        {
            int res = 0;
            try
            {
                res = DBHelper.GetExcuet(string.Format("delete from eevent where eno='{0}'", selectedItem));
            }
            catch
            {

            }
            finally
            {
                var dt = eventdt();
                callback.ShowResults(dt, res);
            }
        }

        public void EventInsert(string[] selectedItem)
        {
            int res = 0;
            try
            {
                res = DBHelper.GetExcuet(string.Format("insert into eevent values ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')", selectedItem[0], selectedItem[1], selectedItem[2], selectedItem[3], selectedItem[4], selectedItem[5], selectedItem[6]));
            }
            catch
            {

            }
            finally
            {
                var dt = eventdt();
                callback.ShowResults(dt, res);
            }
        }

        public void EventSelectAll()
        {
            var dt = eventdt();
            callback.ShowResults(dt, 1);
        }

        public DataTable eventdt()
        {
            var dt = DBHelper.GetDataSet("select eno '比赛编号', ename '比赛名称', etime '比赛时间', fno '项目编号', jno '裁判编号', sno '场地编号', esex '性别组别' from eevent");
            return dt;
        }

        public void EventSelectKeyword(string keyword)
        {
            var dt = DBHelper.GetDataSet(String.Format("select eno '比赛编号', ename '比赛名称', etime '比赛时间', fno '项目编号', jno '裁判编号', sno '场地编号', esex '性别组别' from eevent where eno like '%{0}%' or ename like '%{0}%' or etime like '%{0}%' or fno like '%{0}%' or jno like '%{0}%' or sno like '%{0}%' or esex like '%{0}%'", keyword));
            callback.ShowResults(dt, 1);
        }

        public void EventUpdate(string[] selectedItem)
        {
            int res = 0;
            try
            {
                int a = DBHelper.GetExcuet(string.Format("delete from eevent where eno='{0}'", selectedItem));
                int b = DBHelper.GetExcuet(string.Format("insert into eevent values ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')", selectedItem[0], selectedItem[1], selectedItem[2], selectedItem[3], selectedItem[4], selectedItem[5], selectedItem[6]));
                res = Math.Min(a, b);
            }
            catch
            {

            }
            finally
            {
                var dt = eventdt();
                callback.ShowResults(dt, res);
            }
        }

        public void SubjectDelete(string selectedItem)
        {
            int res = 0;
            try
            {
                res = DBHelper.GetExcuet(string.Format("delete from form where Fno='{0}'", selectedItem));
            }
            catch
            {

            }
            finally
            {
                var dt = subjectdt();
                callback.ShowResults(dt, res);
            }

        }
        public void SubjectInsert(string[] selectedItem)
        {
            int res = 0;
            try
            {
                res = DBHelper.GetExcuet(string.Format("insert into form values ('{0}', '{1}')", selectedItem[0], selectedItem[1]));
            }
            catch
            {

            }
            finally
            {
                var dt = subjectdt();
                callback.ShowResults(dt, res);
            }
        }

        public DataTable subjectdt()
        {
            var dt = DBHelper.GetDataSet("select Fno '项目编号', fname '项目名称' from form");
            return dt;
        }
        public void SubjectSelectAll() // 实现服务接口中的方法
        {
            //var dt = DBHelper.GetDataSet("select Fno '项目编号', fname '项目名称' from form");
            var dt = subjectdt();
            callback.ShowResults(dt, 1); // 使用回调通道通过回调接口将消息发送回客户端
        }

        public void SubjectSelectKeyword(string keyword)
        {
            var dt = DBHelper.GetDataSet(String.Format("select Fno '项目编号', fname '项目名称' from form where Fno like '%{0}%' or fname like '%{0}%'", keyword));
            callback.ShowResults(dt, 1);
        }

        public void SubjectUpdate(string[] selectedItem)
        {

            int res = 0;
            try
            {
                res = DBHelper.GetExcuet(string.Format("delete from form where Fno='{0}'", selectedItem[0]));
                DBHelper.GetExcuet(string.Format("insert into form values ('{0}', '{1}')", selectedItem[0], selectedItem[1]));
            }
            catch
            {

            }
            finally
            {
                var dt = subjectdt();
                callback.ShowResults(dt, res);
            }
        }
    }
}
